
//---------------------------------------------------------------------------
#include "rendering/pg2_renderer.h"
//---------------------------------------------------------------------------
#include "pg2_vertexbuffer.h"
//---------------------------------------------------------------------------
#include <d3dx9.h>
//---------------------------------------------------------------------------
using namespace pg2;
//---------------------------------------------------------------------------
// vertex types definitions
#define D3DFVF_COLORVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
//---------------------------------------------------------------------------
// Engine to DirectX primitives enum mapping
D3DPRIMITIVETYPE g_ePrimitivesMapping[PrimitiveCount] = 
{
	D3DPT_TRIANGLELIST,		// TriangleList
	D3DPT_TRIANGLESTRIP,	// TriangleStrip
	D3DPT_POINTLIST,		// PointList
    D3DPT_LINELIST,			// LineList
    D3DPT_LINESTRIP,		// LineStrip
	D3DPT_TRIANGLEFAN		// TriangleFan
};
//---------------------------------------------------------------------------
Renderer::Renderer ()
:
m_pkD3D(NULL),
m_pkDevice(NULL),
m_pkColorVB(NULL)
{
	// nothing to do
}
//---------------------------------------------------------------------------
Renderer::~Renderer ()
{
	delete m_pkColorVB;
	m_pkColorVB = NULL;
}
//---------------------------------------------------------------------------
bool Renderer::init (HWND hWnd)
{
	m_pkD3D = Direct3DCreate9(D3D_SDK_VERSION);

	if(!m_pkD3D)
	{
		return false;
	}

	D3DDISPLAYMODE displayMode;
	HRESULT hr;

	hr = m_pkD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);

	if(hr != D3D_OK)
	{
		return false;
	}

	D3DPRESENT_PARAMETERS d3DPresentParameters;
	ZeroMemory(&d3DPresentParameters, sizeof(d3DPresentParameters));

	d3DPresentParameters.BackBufferFormat = displayMode.Format;
	d3DPresentParameters.Windowed = true;
	d3DPresentParameters.BackBufferCount = 1; 
	d3DPresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3DPresentParameters.hDeviceWindow = 0; 
	d3DPresentParameters.Flags = 0;
	d3DPresentParameters.EnableAutoDepthStencil = TRUE;
	d3DPresentParameters.AutoDepthStencilFormat = D3DFMT_D16;
	d3DPresentParameters.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	d3DPresentParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

	hr = m_pkD3D->CreateDevice(
							  D3DADAPTER_DEFAULT,
 							  D3DDEVTYPE_HAL,							
							  hWnd,
							  D3DCREATE_HARDWARE_VERTEXPROCESSING,
							  &d3DPresentParameters,
							  &m_pkDevice
	);

	if(hr != D3D_OK)
	{
		return false;
	}

	// disable lighting
	m_pkDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

	// create vertex buffer
	 m_pkColorVB = new VertexBuffer<ColorVertex>(m_pkDevice, D3DFVF_COLORVERTEX);

	return true;
}
//---------------------------------------------------------------------------
void Renderer::beginFrame ()
{
	m_pkDevice->BeginScene();
	m_pkDevice->Clear(0, 
					  NULL, 
					  D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
					  D3DCOLOR_XRGB(255,255,0),		// background color
					  1.0f, 
					  0
	);
}
//---------------------------------------------------------------------------
void Renderer::endFrame ()
{
	m_pkDevice->EndScene();
	m_pkDevice->Present(NULL, NULL, NULL, NULL);
}
//---------------------------------------------------------------------------
void Renderer::draw (const ColorVertex* pakVertexCollection, Primitive ePrim, unsigned int uiVertexCount)
{
	 m_pkColorVB->bind();
	 m_pkColorVB->draw(pakVertexCollection, g_ePrimitivesMapping[ePrim], uiVertexCount);
}
//---------------------------------------------------------------------------
